home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 January: Mac OS SDK / Dev.CD Jan 97 SDK1.toast / Development Kits (Disc 1) / QuickTime / Programming Stuff / Sample Code / Music Architecture / Mixed Bag / BigEasy / BigEasyTextish.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-18  |  8.1 KB  |  516 lines  |  [TEXT/KAHL]

  1. /*
  2.                 File:        BigEasyTextish.c
  3.             
  4.                 Contains:    xxx put contents here xxx
  5.             
  6.                 Written by:    xxx put writers here xxx
  7.             
  8.                 Copyright:    © 1990-1992 by Apple Computer, Inc., all rights reserved.
  9.             
  10.     This file is used in these builds: Warhol
  11.  
  12.                 Change History (most recent first):
  13.             
  14.         <9+>      5/6/93    dvb        String->Number
  15.          <9>      1/7/93    dvb        New functions (TruncateString).
  16.          <8>     1/20/92    dvb        This kind of FixMath is KRAZEE!
  17.          <7>    11/12/91    dvb        General fixation
  18.          <6>     11/4/91    JB        Use fixmath instead of floats
  19.          <5>      6/3/91    dvb        Just Hackin'.
  20.          <4>     5/23/91    PH        THINK C 5
  21.          <3>     4/25/91    JB        Changing to new THINK_C interface files
  22.          <2>    11/16/90    dvb        Remove drawcstring
  23.         <1>        11/16/90    dvb        Check In! Fresh after Camplejohn Soup!
  24.             
  25.                 To Do:
  26.             */
  27.  
  28. /* file: BigEasyTextish.c
  29.  *
  30.  * Started 13 July 1989, more or less.
  31.  *
  32.  * A set of routines for converting and
  33.  * displaying textish things on the Mac.
  34.  *
  35.  */
  36.  
  37.  
  38.  
  39. /************************************
  40. * Inclusions
  41. ************************************/
  42.  
  43. #define BigEasyTextish
  44.  
  45. #include <QuickDraw.h>
  46. #include <Packages.h>
  47. #include <Memory.h>
  48. #include <FixMath.h>
  49.  
  50. #include "BigEasyTextish.h"
  51.  
  52.  
  53. /************************************
  54. * Limits
  55. ************************************/
  56.  
  57. /************************************
  58. * Types and globals
  59. ************************************/
  60. static char dHexChars[] = "0123456789ABCDEF";
  61.  
  62. /************************************
  63. * Routines
  64. ************************************/
  65. void AnyBaseToPString(long n,short b,short g,StringPtr c)
  66. /*
  67.   * Convert 32 bit number n to
  68.   *  string c, in number base b.
  69.   * g is the format: 0 - any length signed
  70.   * positive: unsigned with leading zeros
  71.   * to width g.
  72.   */
  73.     {
  74.     register short charCount;
  75.     register StringPtr w;
  76.     register unsigned long x;        /* unsigned number for divs    */
  77.  
  78.     if(b<2 || b>16)                /* a ridiculous number base?    */
  79.         {
  80.         c[0] = 0;                /* mark as empty string        */
  81.         return;                /* bye.                    */
  82.         }
  83.  
  84.     if(g == 0)
  85.         {
  86.         charCount = 1;            /* initially, 1 char long        */
  87.  
  88.         if(n < 0)                /* number negative?        */
  89.             {
  90.             charCount++;        /* one more char            */
  91.             n = -n;                /* make it positive            */
  92.             c[1] = '-';            /* drop in a minus sign        */
  93.             if(n < 0)
  94.                 n --;            /* most negative? pin it. */
  95.             }
  96.  
  97.         x = n;                /* find out how many digits    */
  98.         while(x /= b)
  99.             charCount++;
  100.  
  101.         c[0] = charCount;        /* number of digits, plus sign    */
  102.         w = c+c[0];            /* we'll walk backwards        */
  103.         x = n;
  104.         do
  105.             {
  106.             *w-- = dHexChars[x%b];
  107.             x /= b;
  108.             } while(x);
  109.         }
  110.     else if(g > 0)
  111.         {                    /* leading zero format        */
  112.         c[0] = g;            /* poke in number of chars    */
  113.         x = n;                /* get our number unsigned    */
  114.         w = &c[g];            /* point to end of string        */
  115.  
  116.         while(g--)
  117.             {
  118.             *w-- = dHexChars[x%b];
  119.             x /= b;
  120.             }
  121.         }
  122.     else if(g < 0)
  123.         {                    /* leading space format   */
  124.         g = -g;
  125.         c[0] = g;                /* poke in number of chars    */
  126.         x = n;                /* get our number unsigned    */
  127.         w = &c[g];            /* point to end of string        */
  128.         
  129.         while(g--)
  130.             {
  131.             *w-- = dHexChars[x%b];
  132.             x /= b;
  133.             }
  134.         g = c[0]-1;
  135.         w = &c[1];
  136.         while(g--)
  137.             {
  138.             if (*w == '0')
  139.                 *w++ = 0xca;
  140.             else
  141.                 break;
  142.             }
  143.         }
  144.     else                    /* negative format passed    */
  145.         c[0] = 0;
  146.     }
  147.  
  148. void DrawNum(n)
  149. /*
  150.   * Draw number n as a signed long, in decimal
  151.   */
  152.     long n;
  153.     {
  154.     Str31 c;
  155.  
  156.     AnyBaseToPString(n,10,0,c);
  157.     DrawString((StringPtr)c);
  158.     }
  159.  
  160. long dTens[] =
  161.     {
  162.     1,
  163.     10,
  164.     100,
  165.     1000,
  166.     10000,
  167.     100000,
  168.     1000000,
  169.     10000000,
  170.     100000000,
  171.     1000000000
  172.     };
  173.  
  174.  
  175. void FixedPointToPString(long n,short g,short p,StringPtr s)
  176. /*
  177.   * Draw number n in fixed point, with
  178.   * g decimal places, with p fractional
  179.   * binary places.
  180.   */
  181.     {
  182.     Str31 c;
  183.     long x;        /* This should float to avoid overflows, but JB says, No! */
  184.  
  185.     s[0] = 0;
  186.  
  187.     if( n < 0 )
  188.         {
  189.         ConcatenatePStrings(s,"\p-");
  190.         n = -n;
  191.         if(n < 0)
  192.             n--;        /* pin most negative */
  193.         }
  194.  
  195.     AnyBaseToPString(n>>p,10,0,c);
  196.     ConcatenatePStrings(s,c);
  197.     ConcatenatePStrings(s,"\p.");
  198.  
  199.     if(g>9)
  200.         g = 9;
  201.  
  202.     if(g)
  203.         {
  204.         x = (n & ((1L<<p)-1) );
  205.         x *= dTens[g];
  206.         x /= (x,1L<<p);
  207.  
  208.         AnyBaseToPString(x,10,g,c);
  209.         ConcatenatePStrings(s,c);
  210.         }
  211.     }
  212.  
  213.  
  214. void PStringToFixedPoint(StringPtr s,short g,short p,long *n)
  215.     {
  216.     long x;
  217.     unsigned long y,z,z10;
  218.     short count;
  219.     Boolean decimal;
  220.     Boolean sign;
  221.  
  222.     count = *s++;
  223.     x = 0;
  224.  
  225.     decimal = false;
  226.     sign = false;
  227.  
  228.     z = (1L<<p);
  229.     z10 = 10;
  230.  
  231.     while(count-- && z)
  232.         {
  233.         y = *s;
  234.         if(y == '.')
  235.             decimal = true;
  236.         else if (y == '-')
  237.             sign = true;
  238.         else
  239.             {
  240.             y = y - '0';
  241.             if(y < 10)
  242.                 {
  243.                 if(!decimal)
  244.                     {
  245.                     x *= 10;
  246.                     x += y << p;
  247.                     }
  248.                 else
  249.                     {
  250.                     x += (y * z + z10/2) / z10;
  251.                     z10 *= 10;
  252.                     }
  253.                 }
  254.             }
  255.         s++;
  256.         }
  257.  
  258.     if(sign)
  259.         x = -x;
  260.  
  261.     *n = x;
  262.     }
  263.  
  264. void DrawFixedPoint(long n,short g,short p)
  265. /*
  266.   * Draw number n in fixed point, with
  267.   * g decimal places, with p fractional
  268.   * binary places.
  269.   */
  270.     {
  271.     Str255 s;
  272.  
  273.     FixedPointToPString(n,g,p,s);
  274.     DrawString(s);
  275.     }
  276.  
  277.  
  278. void DrawFixed(long n,short g)
  279. /*
  280.   * Draw number n in fixed point, with
  281.   * g decimal places.
  282.   */
  283.     {
  284.     DrawFixedPoint(n,g,16);
  285.     }
  286.  
  287. void DrawFixedPointJustified(long n,short g,short p,short h)
  288. /*
  289.   * Draw number n in fixed point, with
  290.   * g decimal places, with p fractional
  291.   * binary places.
  292.   */
  293.     {
  294.     Str255 s;
  295.     Str31 c;
  296.     long x;
  297.  
  298.     s[0] = 0;
  299.  
  300.     if( n < 0 )
  301.         {
  302.         ConcatenatePStrings(s,"\p-");
  303.         n = -n;
  304.         if(n < 0)
  305.             n --;            /* most negative? pin it. */
  306.         }
  307.  
  308.     AnyBaseToPString(n>>p,10,-h,c);
  309.     ConcatenatePStrings(s,c);
  310.     ConcatenatePStrings(s,"\p.");
  311.  
  312.     if(g>9)
  313.         g = 9;
  314.  
  315.     if(g)
  316.         {
  317.         
  318.         x = (n & ((1L<<p)-1) );
  319.         x *= dTens[g];
  320.         x /= (x,1L<<p);
  321.  
  322.         AnyBaseToPString(x,10,g,c);
  323.         ConcatenatePStrings(s,c);
  324.         }
  325.  
  326.     DrawString(s);
  327.     }
  328.  
  329.  
  330. void DrawFixedJustified(long n,short g,short h)
  331. /*
  332.   * Draw number n in fixed point, with
  333.   * g decimal places.
  334.   */
  335.     {
  336.     DrawFixedPointJustified(n,g,16,h);
  337.     }
  338.  
  339.  
  340.  
  341. void DrawFrac(long n,short g)
  342. /*
  343.   * Draw number n as a frac (2.30), with
  344.   * g decimal places.
  345.   */
  346.     {
  347.     DrawFixedPoint(n,g,30);
  348.     }
  349.  
  350. void DrawHexLong(unsigned long n)
  351. /*
  352.   * Draw an 8-digit hex number
  353.   * with leading zeroes
  354.   */
  355.     {
  356.     Str31 c;
  357.  
  358.     AnyBaseToPString(n,16,8,c);
  359.     DrawString((StringPtr)c);
  360.     }
  361.  
  362. void DrawHexShort( unsigned short n)
  363. /*
  364.   * Draw a 4-digit hex number
  365.   * with leading zeroes
  366.   */
  367.     {
  368.     Str31 c;
  369.  
  370.     AnyBaseToPString(n,16,4,c);
  371.     DrawString(c);
  372.     }
  373.  
  374. void DrawHexByte(unsigned char n)
  375.     {
  376.     Str31 c;
  377.  
  378.     AnyBaseToPString(n,16,2,c);
  379.     DrawString(c);
  380.     }
  381.  
  382. void CToPString(register char *c,register StringPtr p)
  383.     {
  384.     register short i;
  385.     register StringPtr pWalker;
  386.  
  387.     i = 0;
  388.     pWalker = p+1;
  389.     while(*pWalker++ = *c++)
  390.         i++;
  391.     *p = i;
  392.     }
  393.  
  394. void DrawCR(void)
  395. /*
  396.   * Bump penposition down a line, to
  397.   * gLeftMargin, and +gLineHeight.
  398.   */
  399.     {
  400.     Point p;
  401.  
  402.     GetPen(&p);
  403.     p.h = gLeftMargin;
  404.     p.v += gLineHeight;
  405.     MoveTo(p.h,p.v);
  406.     }
  407.  
  408.  
  409.  
  410. short CStringWidth(char *c)
  411. /*
  412.   * Do a StringWidth on a C string
  413.   */
  414.     {
  415.     Str255 p;
  416.  
  417.     CToPString(c,p);
  418.     return StringWidth((StringPtr)p);
  419.     }
  420.  
  421.  
  422. void CopyPString(StringPtr dest,StringPtr src)
  423.     {
  424.     if(dest && src)
  425.         BlockMove(src,dest,(*src) + 1);
  426.     }
  427.  
  428.  
  429. void ConcatenatePStrings(StringPtr dest,StringPtr src)
  430.     {
  431.     BlockMove(src+1,dest+1+(*dest),*src);
  432.     *dest += *src;
  433.     }
  434.  
  435.  
  436. void DrawStringRight(StringPtr s)
  437. /*
  438.  * Draw the string from the current pen position
  439.  * off to the left; leave the pen where it started.
  440.  */
  441.     {
  442.     Point po;
  443.  
  444.     GetPen(&po);
  445.     Move(-StringWidth(s),0);
  446.     DrawString(s);
  447.     }
  448.  
  449. void DrawStringCenter(StringPtr s)
  450. /*
  451.  * Draw the string centered at
  452.  * the current pen position; 
  453.  * leave the pen where it started.
  454.  */
  455.     {
  456.     Point po;
  457.  
  458.     GetPen(&po);
  459.     Move(-StringWidth(s)/2,0);
  460.     DrawString(s);
  461.     MoveTo(po.h,po.v);
  462.     }
  463.  
  464. void DrawStringLeft(StringPtr s)
  465. /*
  466.  * Draw the string from the current pen position
  467.  * off to the right; leave the pen where it started.
  468.  */
  469.     {
  470.     Point po;
  471.  
  472.     GetPen(&po);
  473.     DrawString(s);
  474.     MoveTo(po.h,po.v);
  475.     }
  476.  
  477.  
  478. void TruncateString(StringPtr s, short width)
  479. /*
  480.  * Draw the string limited to the width passed,
  481.  * Truncated if necessary with a trailing “…”.
  482.  */
  483.     {
  484.     while(StringWidth(s) > width)
  485.         {
  486.         s[0] --;
  487.         s[s[0]] = '…';
  488.         }
  489.     }
  490.  
  491.  
  492. void DrawStringTruncated(StringPtr s, short width)
  493. /*
  494.  * Draw the string limited to the width passed,
  495.  * Truncated if necessary with a trailing “…”.
  496.  */
  497.     {
  498.     Str255 s2;
  499.  
  500.     CopyPString(s2,s);
  501.     TruncateString(s2,  width);
  502.     DrawString(s2);
  503.     }
  504.  
  505.  
  506.  
  507. void OSTypeToString(StringPtr dest,OSType x)
  508.     {
  509.     dest[0] = 4;
  510.     dest[1] = (x>>24);
  511.     dest[2] = (x>>16);
  512.     dest[3] = (x>>8);
  513.     dest[4] = x;
  514.     }
  515.  
  516.